package com.xudow.app.directseeding.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bokecc.sdk.mobile.live.replay.pojo.ReplayAnswerMsg;
import com.bokecc.sdk.mobile.live.replay.pojo.ReplayQAMsg;
import com.bokecc.sdk.mobile.live.replay.pojo.ReplayQuestionMsg;
import com.xudow.app.R;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;

public class MyReplayQAListViewAdapter extends BaseAdapter {
private Context context;
private List<ReplayQAMsg> qaMsgs;

public MyReplayQAListViewAdapter(Context context, TreeSet<ReplayQAMsg> qaMsgs) {
		this.context = context;
		this.qaMsgs = new ArrayList<ReplayQAMsg>(qaMsgs);
		Log.i("huifangs", "MyReplayQAListViewAdapter" + qaMsgs.size());


		}
@Override
public boolean areAllItemsEnabled() {
		return false;
		}

@Override
public boolean isEnabled(int position) {
		return false;
		}

@Override
public int getCount() {
	Log.i("huifangs", "getCount" + qaMsgs.size());
		return qaMsgs.size()-1;
		}

@Override
public Object getItem(int position) {

		return qaMsgs.get(position);
		}

@Override
public long getItemId(int position) {
		return position;
		}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
		viewHolder = new ViewHolder();
		convertView = LayoutInflater.from(context).inflate(R.layout.lv_qa_view, parent, false);
			viewHolder.answer = (TextView) convertView.findViewById(R.id.tv1);
			viewHolder.answer2 = (TextView) convertView.findViewById(R.id.tv2);
			viewHolder.question = (TextView) convertView.findViewById(R.id.tv3);
			viewHolder.question2 = (TextView) convertView.findViewById(R.id.tv4);
		convertView.setTag(viewHolder);
		} else {
		viewHolder = (ViewHolder) convertView.getTag();
		}

		ReplayQAMsg qaMsg = qaMsgs.get(position);

		ReplayQuestionMsg question = qaMsg.getReplayQuestionMsg();
	viewHolder.question2.setTextColor(context.getResources().getColor(R.color.public_chat));
		viewHolder.question.setText(question.getQuestionUserName() + "问：" );
	viewHolder.question2.setText(question.getContent());

		TreeSet<ReplayAnswerMsg> answers = qaMsg.getReplayAnswerMsgs();

		if (answers.size() < 1) {
		viewHolder.question.setVisibility(View.GONE);
		viewHolder.answer.setVisibility(View.GONE);
		return convertView;
		}

		 StringBuilder sb = new StringBuilder();
	     StringBuilder sb2 = new StringBuilder();
		for (ReplayAnswerMsg answer: answers) {
		sb.append(answer.getContent() + ",");
			sb2.append(answer.getUserName() + "答：" );
		}
//		if (sb.length() > 2) {
			viewHolder.answer2.setTextColor(context.getResources().getColor(R.color.public_chat));
		viewHolder.answer.setText(sb2.substring(0, sb.length() - 1));

			viewHolder.answer2.setText(sb.substring(0, sb.length() - 1));
//		} else {
//		viewHolder.answer.setText("");
//		}

		viewHolder.question.setVisibility(View.VISIBLE);
		viewHolder.answer.setVisibility(View.VISIBLE);
		return convertView;
		}

private class ViewHolder {
	public TextView question,question2;
	public TextView answer,answer2;
}
}
